<template>
  <div class="sf-tab-box">
    <div :class="['sf-tab', { 'sf-tab-bor': hideTab === 'list' }]">
      <div v-if="fromList.length > 0" :style="{ 'line-height': lineHeihgt }" class="cusType">
        <span v-if="cusTypeTitle" class="cusType-title">{{ cusTypeTitle }}</span>
        <el-dropdown trigger="click" size="medium" @command="changeCusType">
          <span class="el-dropdown-link cursor">
            <b>
              {{ cusType }}
              <i v-if="fromList.length > 1" class="el-icon-arrow-down el-icon--right" />
            </b>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-for="(item, index) in fromList" :key="index" :command="item">{{ item }}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div v-else class="cusType"></div>
      <div class="sf-btn">
        <!-- 右边按钮区域 -->
        <WholeBtn :show-check="showCheck" :show-type="showType" :placeholder="placeholder" :filter-list="filterList" :filter-sort="filterSortConfig" @hideOrShow="handleChange" />
      </div>
    </div>
    <div v-if="hideTab === 'list'" class="sf-tab" style="padding: 5px 20px;">
      <!-- tab菜单标签 -->
      <div class="items-box">
        <div
          v-for="item in tabs"
          :key="item.value"
          :style="{ 'line-height': lineHeihgt }"
          class="tab-item"
          :class="{ 'tab-active': activeName === item.value }"
          @click="handleClick(item.value, item)"
        >
          <template v-if="item.textType === 'badge' && (item.zeroShow || (!item.zeroShow && item.num > 0))">
            <el-badge :value="item.num" :max="item.max || 9999" class="badge-item">
              <span>{{ item.name }}</span>
            </el-badge>
          </template>
          <template v-else>
            <span class="num-name">{{ item.name }}</span>
            <span v-if="item.num" class="num-text">{{ item.num }}</span>
          </template>
        </div>
      </div>

      <div class="sf-btn">
        <slot />
      </div>
    </div>
  </div>
</template>
<script>
import WholeBtn from '@/components/Btn/wholeBtn.vue'
export default {
  components: {
    WholeBtn
  },

  provide() {
    return {
      tabFilter: this
    }
  },

  props: {
    tabs: {
      type: Array,
      default: () => []
    },
    activeName: {
      type: [String, Number],
      default: ''
    },
    // 左侧标题
    cusTypeTitle: {
      type: String,
      default: ''
    },
    // 左侧下拉数据
    fromList: {
      type: Array,
      default: () => []
    },
    // 是否返回所有数据
    isAllReturn: {
      type: Boolean,
      default: false
    },
    lineHeihgt: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: ''
    },
    // 筛选项
    filterList: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 筛选中增加按钮
    filterSort: {
      type: Array,
      default: () => {
        return []
      }
    },
    showCheck: {
      type: Boolean,
      default: false
    },
    showType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      cusType: '',
      hideTab: 'list',
      radio4: '列表',
      radio1: ''
    }
  },
  watch: {
    fromList: {
      handler(v) {
        if (v) {
          this.cusType = v[0]
        }
      },
      immediate: true
    },
    filterSort: {
      handler(v) {
        this.filterSortConfig = v
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    handleClick(v, data) {
      if (this.isAllReturn) {
        this.$emit('handleTabClick', {
          activeName: v,
          data
        })
      } else {
        this.$emit('handleTabClick', v)
      }
    },
    changeCusType(v) {
      this.cusType = v
      this.$emit('handelCusType', v)
    },
    changeFilter(v) {
      this.$emit('remotefilterChange', JSON.parse(JSON.stringify(v)))
    },
    handleChange(value) {
      this.$emit('hideOrShow', value)
    }
  }
}
</script>
<style lang="scss" scoped>
.sf-tab-box {
  border-radius: 6px;
  margin-bottom: 6px;
  overflow: hidden;
  background-color: #ffffff;
  box-shadow: 0px 0px 6px 0px rgb(0 0 0 / 8%);
  .sf-tab-bor {
    border-bottom: 1px solid #ccc;
    line-height: 40px;
  }
}
.sf-tab {
  color: #333;
  padding: 5px 30px;
  display: flex;
  .items-box {
    flex: 1 1 auto;
  }
  .tab-item {
    // line-height: 45px;
    display: inline-block;
    margin: 0 15px 0 0;
    cursor: pointer;
    position: relative;
    font-size: 16px;
    letter-spacing: 0px;
    color: #727373;
    padding: 10px;
    .num-text {
      background: #f2f2ff;
      border-radius: 8px;
      color: rgba(0, 0, 0, 0.6);
      font-weight: 500;
      padding: 0 6px;
    }
    .el-icon-back {
      font-size: 17px;
      font-weight: bold;
      margin-right: 10px;
    }
    &.line-right::after {
      content: '';
      position: absolute;
      right: -20px;
      top: 12px;
      height: 40px;
      width: 1px;
      background-color: #ebeef5;
      border: none;
    }
    &:hover {
      color: rgba(0, 0, 0, 0.88);
      background: rgba(128, 140, 166, 0.08);
      border-radius: 8px;
      .num-text {
        color: rgba(0, 0, 0, 0.6);
      }
    }
  }
  .tab-active {
    font-weight: 500;
    .num-text {
      color: #409EFF;
    }
    .num-name {
      position: relative;
    }
    .num-name::after {
      content: '';
      position: absolute;
      left: 50%;
      bottom: -10px;
      width: 100%;
      transform: translate(-50%, 0);
      width: 24px;
      height: 3px;
      background: #409EFF;
      border-radius: 2px;
    }
  }
  .sf-btn {
    line-height: 40px;
  }
}

.cusType {
  flex: 1 1 auto;
  line-height: 40px;
  .el-dropdown {
    font-size: 16px;
  }
  .cusType-title {
    font-size: 16px;
  }
}
.badge-item {
  ::v-deep {
    .el-badge__content {
      top: 15px;
    }
  }
}
</style>
